$(function () {

    //音频播放
    var progressWrap = document.getElementById("progressWrap");
    var playProgress = document.getElementById("playProgress");
    var a1=document.getElementById('a1');// 目标 音频元素
    var showProgress=document.getElementById('showProgress');
    var btn=document.getElementById('btn');
    var did=window.sessionStorage['did'];
    var thisTotalplay=window.sessionStorage['thisTotalplay'];


    //发起ajax请求 获取 音频详情数据
    $.ajax({
        type:'get',
        url:'http://api.heardtech.com/Get/MusicInfo?id='+did+"&token_key=c4ca4238a0b923820dcc509a6f75849b",
        dataType:'json',
        success: function (data) {
            //console.log(data);
            var result=data.data;
            if(a1.src===''){
                a1.src=result.voiceurl;
            }
        },
        error: function (error) {
            console.log(error);
        }

    });

    //给播放按钮点击事件
    $('#btn').on('tap', function () {
        console.log(a1);
        play();
    });
    // video的播放条
    function getProgress(){
        var percent = a1.currentTime / a1.duration;
        playProgress.style.width = percent * (progressWrap.offsetWidth) - 2 + "px";
        //showProgress.innerHTML = (percent * 100).toFixed(1) + "%";
        console.log('定时器启动');
    }
    //http://zhubo.heardtech.com/Public/music/2017-07-06/595db4b815617.mp3
    //// 控制video的播放
    function play(){
        if ( a1.paused){
            if ( a1.ended ){
                a1.currentTime = 0;
            }
            a1.play();
            btn.setAttribute('class','play_icon');
            //btn.innerHTML = "暂停";
            progressFlag = setInterval(getProgress, 60);
            timer=setTimeout(function () {
                $(showProgress).addClass('preload-line');
            },800)
        }
        else{
            a1.pause();
            btn.setAttribute('class','pause_icon');
            //btn.innerHTML = "播放";
            clearInterval(progressFlag);
            progressFlag=null;
        }
    }


    //监听 a1 音频 结束清空 定时器
    a1.addEventListener('ended',function(){
        clearInterval(progressFlag);
        progressFlag=null;
    })
    //视频播放中事件
    a1.addEventListener('timeupdate', function() {
        ////更新进度条
        //var percentage = 100 * currentPos / that.vDuration;
        ////设置宽度
        //that.vC.querySelector('.timeBar').style.width = percentage + '%';
        var currentPos = a1.currentTime;//获取当前播放的位置
        document.querySelector('.current').innerHTML = stom(currentPos);
    });
    //加载元数据的时候 获取 音频总 时间
    a1.addEventListener('loadedmetadata',function(){
        document.querySelector('.duration').innerHTML = stom(this.duration);
    });

    //音频 时间装换函数
    function stom(t) {
        var m = Math.floor(t / 60);
        m < 10 && (m = '0' + m);
        return m + ":" + (t % 60 / 100).toFixed(2).slice(-2);
    }



    //发送请求 获得 服务器端 初始化音频播放次数
    //console.log(thisTotalplay);
    $.ajax({
        type:'get',
        url:"http://api.heardtech.com/Get/MusicSetInc?sum="+thisTotalplay+"&id="+did+"&token_key=c4ca4238a0b923820dcc509a6f75849b",
        success: function (data) {
            console.log(data);
            thisTotalplay=data.data;
           // console.log(--thisTotalplay);
            $("#total—play").html(--thisTotalplay+"万");//播放次数
           // console.log("没减得时候"+data.data);
            //window.sessionStorage['thisTotalplay']=thisTotalplay;
        },
        //id:NhjeEaO0O0Or
        error: function (data) {
            // console.log(error);
            alert('请求出错！');
            console.log(data)
        }
    });





















})